<?php

/**
* @Author:  	Daniel Dinh
* @Email:   	webdevelopmentvn@gmail.com
* @Link: 		http://www.joomsoft.net
* @Version: 	1.0
* @copyright:	Joomsoft.net
* @license:		Commercial
*/

// No direct access.
defined('_JEXEC') or die;

jimport('joomla.application.component.controlleradmin');

class JSNPluginControllerPlugins extends JControllerAdmin
{
	/**
	 * @var		string	The prefix to use with controller messages.
	 * @since	1.6
	 */
	protected $text_prefix = 'COM_JSNPLUGIN_PLUGINS';

	/**
	 * Constructor.
	 *
	 * @param	array An optional associative array of configuration settings.
	 * @see		JController
	 * @since	1.6
	 */
	public function __construct($config = array())
	{
		parent::__construct($config);
	}

	/**
	 * Proxy for getModel.
	 * @since	1.6
	 */
	public function getModel($name = 'Plugin', $prefix = 'JSNPluginModel', $config = array('ignore_request' => true))
	{
		$model = parent::getModel($name, $prefix, $config);
		return $model;
	}
	
	/**
	 * @since	1.6
	 */
	 
	function export()
	{
		require_once JPATH_COMPONENT . '/helpers/jsnplugin.php';
		$db = & JFactory::getDBO();
		$path = JPATH_COMPONENT.DS;
		$pid = JRequest::getVar('id');
		$db->setQuery("select p.*,g.title as groupname from #__jsnp_plugin p INNER JOIN #__jsnp_group g ON p.id_group = g.id where p.id=$pid");
		$plugin = $db->loadObject();	 
		//get field
		$db->setQuery("select f.*,fd.data from #__jsnp_jsnfield f LEFT JOIN #__jsnp_field_data fd ON f.id=fd.id_field WHERE fd.id_plugin = $pid");
		$fields = $db->loadObjectList();
		//get language
		$db->setQuery("select * FROM #__jsnp_language_data WHERE id_plugin = $pid");
		$langs = $db->loadObjectList();
		//get event
		$db->setQuery("select * FROM #__jsnp_event_data WHERE id_plugin = $pid");
		$events = $db->loadObjectList();
		
		//get plugin name
		$plugin_name = JSNPLuginHelper::getPluginName($plugin);
		// create plugin folder name
		$plugrefix = 'plg_'.$plugin->groupname.'_'.$plugin->title.'_';
		$fields = $this->processFields($fields,$plugrefix);		
		JSNPluginHelper::createPluginFolder($plugin,$langs,$events,$fields);
		//echo $plugin_name;exit;
	}
	function processFields($fields,$plugrefix)
	{
		if($fields)
		{
			$basic = '';
			$advanced = '';
			$fieldstext = '';
			foreach($fields as $field)
			{
				if($field->fieldgroup =="basic")
					$basic .= $this->createCusField($field,$plugrefix);
				if($field->fieldgroup =="advanced")
					$advanced .= $this->createCusField($field,$plugrefix);
			}
			if($basic)
			{
				$fieldstext .= '<fieldset name="basic">'.chr(10);
				$fieldstext .= chr(9).$basic.chr(10);
				$fieldstext .= chr(9). chr(9). chr(9).'</fieldset>';
			}
			if($advanced)
			{
				$fieldstext .= '<fieldset name="advanced">'.chr(10);
				$fieldstext .= chr(9).$basic.chr(10);
				$fieldstext .= chr(9). chr(9). chr(9).'</fieldset>';
				
			}

			return $fieldstext;	
		}
		return ;
	}
	function createCusField($field,$plugrefix)
	{
		$data = json_decode($field->data);
		switch($field->fieldtype)
		{
			case "text":
				$textfield = chr(9).chr(9).chr(9).chr(9).'<field type="text" name="'.$field->title.'" label="'.strtoupper($plugrefix.$field->title).'" default="'.$data->default.'"';
				if($data->size)
					$textfield.= ' size="'.$data->size.'"';
				else
					$textfield.= ' size="40"';
				
				if($required)
				{
					$val = 	'true';
				}
				else
					$val = 'false';
				 $textfield.=' required="'.$val.'" />'.chr(10);
			break;	
			case "list":
			$textfield = chr(9).chr(9).chr(9).chr(9).'<field type="list" name="'.$field->title.'" label="'.strtoupper($plugrefix.$field->title).'" default="'.$data->default.'"';
				if($required)
				{
					$val = 	'true';
				}
				else
					$val = 'false';
				$textfield.=' required="'.$val.'" >'.chr(10);	
				if($field->options)
				{
					$lists = @explode(chr(10),$field->options);
					if($lists)	
					{
						foreach($lists as $opt)
						{
							$arr = explode("|",str_replace(chr(13),'',$opt));
							if(count($arr)==2)
								$textfield.= chr(9).chr(9).chr(9).chr(9). chr(9).'<option value="'.$arr[0].'">'.$arr[1].'</option>'.chr(10);
							else
								$textfield.= chr(9).chr(9).chr(9).chr(9). chr(9).'<option value="'.$arr[0].'">'.$arr[0].'</option>'.chr(10);
						}	
					}
				}	
				
 				 $textfield.= chr(9).chr(9).chr(9).chr(9).'</field>'.chr(10);
			
			break;
			case "boolean":
			$textfield = chr(9).chr(9).chr(9).chr(9).'<field type="list" name="'.$field->title.'" label="'.strtoupper($plugrefix.$field->title).'" default="'.$data->default.'"';
				if($required)
				{
					$val = 	'true';
				}
				else
					$val = 'false';
				 $textfield.=' required="'.$val.'" >'.chr(10);
				 $textfield.= chr(9).chr(9).chr(9).chr(9). chr(9).'<option value="0">JNO</option>'.chr(10);
				 $textfield.= chr(9).chr(9).chr(9).chr(9).chr(9).'<option value="1">JYES</option>'.chr(10);
 				 $textfield.= chr(9).chr(9).chr(9).chr(9).'</field>'.chr(10);
			break;
			case "truefalse":
			$textfield = chr(9).chr(9).chr(9).chr(9).'<field type="list" name="'.$field->title.'" label="'.strtoupper($plugrefix.$field->title).'" default="'.$data->default.'"';
				if($required)
				{
					$val = 	'true';
				}
				else
					$val = 'false';
				 $textfield.=' required="'.$val.'" >'.chr(10);
				 $textfield.= chr(9).chr(9).chr(9).chr(9).chr(9).'<option value="false">JNO</option>'.chr(10);
				 $textfield.= chr(9).chr(9).chr(9).chr(9).chr(9).'<option value="true">JYES</option>'.chr(10);
 				 $textfield.= chr(9).chr(9).chr(9). chr(9).'</field>'.chr(10);
			break;
			default:
			
			$textfield = chr(9).chr(9).chr(9).chr(9).'<field type="'.$field->fieldtype.'" name="'.$field->title.'" label="'.strtoupper($plugrefix.$field->title).'" default="'.$data->default.'"';
				if($data->size)
					$textfield.= ' size="'.$data->size.'"';
				if($required)
				{
					$val = 	'true';
				}
				else
					$val = 'false';
				foreach($data as $key=>$dt)
				{
					if(!in_array($key,array('label','description','default','size','name','options','tyle')))
					{
						$textfield .= ' '.$key.'="'.$dt.'" ';
					}	
				}	
				 $textfield.=' required="'.$val.'" />'.chr(10);
			break;
		}
		return $textfield;	
	}
}
